#coding=utf-8
import requests,json,urllib
import mysql_password

#朵云社区登录接口并返回带token的headers
def login(mobile):
    #通过mysql_password方法获取到该用户的salt加密后的密码
    password = mysql_password.password(mobile)[0]
    s = requests.session()
    mobile_panduan = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/check/mobile?mobile='+mobile)  #判断该号码是否注册
    #判断该账号状态
    # if u'该用户账号已注册' in mobile_panduan.text:
    #     print u'该用户账号已注册'+ str(mobile)
    if u'请在下一步页面进行注册' in mobile_panduan.text:  #合伙人情况
        print u'请在下一步页面进行注册'
        return u'请在下一步页面进行注册'
    if u'该用户账号不存在' in mobile_panduan.text:  #成员情况
        print u'该用户账号不存在'
        return u'该用户账号不存在'
    if u'该用户账号已注册' in mobile_panduan.text:
        login_data = ({"mobile": mobile, "encryptedPassword": password})
        login = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/login', json=login_data,headers={'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate','ProcessorInfo': 'A11-6','AppVersion': '1.0.0',
                       'User-Agent': 'DuoYunCommunity/1.0.0 (iPhone; iOS 12.1.2; Scale/3.00)'
                        ,'Host': 'community-platform-api-test.gdgyun.com'
                        , 'System':'iOS','DeviceName':'SmileSoul','AppName':'DuoYunCommunity',
                       'SystemVersion':'12.1.2','Connection': 'keep-alive','Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, hu-CN;q=0.7'
                       ,'ScreenResolution': '750X1624'})  # 进行登录操作
        if u'登录成功' in login.text:
            #print u'登录成功'+ str(mobile)
            # 返回登录后带token的headers和Uid
            token1 = login.json()["data"]["token"], login.text  # 获取token
            userid = login.json()["data"]["userId"], login.text  # 获取Uid
            token = str(token1[0])
            uid = str(userid[0])
            # 定义headers
            headers = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate','ProcessorInfo': 'A11-6','AppVersion': '1.0.0',
                       'User-Agent': 'DuoYunCommunity/1.0.0 (iPhone; iOS 12.1.2; Scale/3.00)'
                        , 'Uid': uid, 'Authorization': token, 'Host': 'community-platform-api-test.gdgyun.com'
                        , 'System':'iOS','DeviceName':'SmileSoul','AppName':'DuoYunCommunity',
                       'SystemVersion':'12.1.2','Connection': 'keep-alive','Accept-Language': 'zh-Hans-CN;q=1, en-CN;q=0.9, zh-Hant-CN;q=0.8, hu-CN;q=0.7'
                       ,'ScreenResolution': '750X1624'}
            return headers, uid, token
            # index = s.get('http://community-platform-api-test.gdgyun.com/api/community/userInfo/view',headers=headers)  #进入朵云-个人中心页面
            # print index.text
        if u'该用户账号密码输入错误' in login.text:
            print u'该用户账号密码输入错误'
            return u'该用户账号密码输入错误'
    else:
        print u'接口出错！'


if __name__ =='__main__':
    # headers = login('13800010035')[0]
    # for i in range(15):
    #     s = requests.session()
    #     #开始抽奖
    #     start = s.get('http://community-platform-api-test.gdgyun.com/api/community/activity/1/lottery/start',headers=headers)
    #     print start.text
    login('13800010060')
